package id.amikom.jogjabankfinder;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.IBinder;
import android.provider.Settings;

public class GPSHelper extends Service
{
	private final long			UPDATE_INTERVAL		= 5000;			// 5
																		// second
	private final float			UPDATE_RANGE		= 10;				// 10
																		// meter

	private Context				context;

	private LocationManager		locationManager;
	private LocationListener	locationListener;
	private String				provider;
	private Location			location;

	private final int			GPS_PROVIDER		= 0;
	private final int			NETWORK_PROVIDER	= 1;
	private int					PROVIDER_USE		= GPS_PROVIDER;

	public GPSHelper(Context _conContext, LocationManager locManager, LocationListener locListener)
	{
		context = _conContext;
		locationListener = locListener;
		locationManager = locManager;
		setupGPS();
	}

	public void setupGPS()
	{
		switch (PROVIDER_USE)
		{
			case GPS_PROVIDER:
				locationManager.requestLocationUpdates(
						LocationManager.GPS_PROVIDER,
						UPDATE_INTERVAL,
						UPDATE_RANGE,
						locationListener);
				break;
			case NETWORK_PROVIDER:
				locationManager.requestLocationUpdates(
						LocationManager.NETWORK_PROVIDER,
						UPDATE_INTERVAL,
						UPDATE_RANGE,
						locationListener);
				break;
		}

		Criteria criteria = new Criteria();
		provider = locationManager.getBestProvider(criteria, false);
		location = locationManager.getLastKnownLocation(provider);

	}

	public void checkGPS()
	{
		final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		if (!gpsEnabled)
		{
			AlertDialog.Builder alert = new AlertDialog.Builder(context);

			alert.setTitle("Aktifkan GPS..");
			alert.setMessage("Apakah anda akan mengaktifkan GPS anda ?");

			alert.setPositiveButton("YES", new OnClickListener()
			{

				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					// TODO Auto-generated method stub
					enableLocationSettings();
				}
			});

			alert.setNegativeButton("NO", new OnClickListener()
			{

				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					// TODO Auto-generated method stub
					dialog.cancel();
				}
			});

			alert.show();

		}
	}

	protected void enableLocationSettings()
	{
		// TODO Auto-generated method stub
		Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		startActivity(i);

	}

	@Override
	public IBinder onBind(Intent intent)
	{
		// TODO Auto-generated method stub
		return null;
	}

	public LocationListener getLocationListener()
	{
		return locationListener;
	}

	public void setLocationListener(LocationListener locationListener)
	{
		this.locationListener = locationListener;
	}

	public Location getLocation()
	{
		return location;
	}

	public void setLocation(Location location)
	{
		this.location = location;
	}

	public void stopGPS()
	{
		locationManager.removeUpdates(locationListener);
	}

}
